package leetcode.easy;

import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

/**
 * ClassName: $125_IsPalindrome
 * Description:
 * date: 2021-12-13 22:38
 *
 * @author liyifan
 */
public class $125_IsPalindrome {
    public boolean isPalindrome(String s) {
        if (s.length() == 1)
            return true;
        List<Character> characters = new LinkedList<>();
        for (Character c:s.toCharArray()) {
            if (isValidChar(c)) {
                characters.add(c);
            }
        }
        char[] chars = new char[characters.size()];
        for (int i = 0;i < chars.length;i++) {
            chars[i] = characters.remove(0);
        }
        s = String.valueOf(chars);
        s = s.toLowerCase();
        if (s.length() == 0 || s.length() == 1)
            return true;

        int left = 0;
        int right = s.length() - 1;
        while (left < right) {
            if (s.charAt(left) == s.charAt(right)) {
                left++;
                right--;
            } else {
                return false;
            }
        }
        return true;
    }

    public boolean isValidChar(char c){
        return Character.isAlphabetic(c) || Character.isDigit(c);
    }
}
